//
//  ZLPNetWorkTool.m
//  NetWorkTool
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 赵连鹏. All rights reserved.
//

#import "ZLPNetWorkTool.h"
#import <AFNetworking.h>

@implementation ZLPNetWorkTool

+ (void)getWithURL:(NSString *)URLStr success:(SUCCESS_BLOCK)successBlock failure:(FAILURE_BLOCK)failureBlock {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:URLStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failureBlock(error);
        
    }];
//    [manager GET:URLStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        successBlock(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        failureBlock(error);
//        
//    }];
}

+ (void)postWithURL:(NSString *)URLStr parameters:(id)paeameters success:(SUCCESS_BLOCK)successBlock failure:(FAILURE_BLOCK)failureBlock {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager POST:URLStr parameters:[self convertStrToBodyDic:paeameters] success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        failureBlock(error);
        
    }];
//    [manager POST:URLStr parameters:[self convertStrToBodyDic:paeameters] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        successBlock(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//       
//        failureBlock(error);
//        
//    }];
    
}

+ (NSDictionary *)convertStrToBodyDic:(NSString *)bodyStr
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSArray *paraArray = [bodyStr componentsSeparatedByString:@"&"];
    for (NSString *tempStr in paraArray) {
        NSArray *keyValueArray = [tempStr componentsSeparatedByString:@"="];
        [dic setObject:keyValueArray[1] forKey:keyValueArray[0]];
    }
    
    NSDictionary *bodyDic = [NSDictionary dictionaryWithDictionary:dic];
    
    return bodyDic;
}

+ (void)judgeNetState {
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    [manager startMonitoring];
    
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        

        if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前为WIFI环境" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
               
                
                
            }];
            [alert addAction:action];
            
            
            
            
        } else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前为2G/3G/4G网络,建议在WIFI环境下使用" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alert addAction:action];
            
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前没有网络,请确认网络状态" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alert addAction:action];

        }
        
        
        
    }];
    [manager stopMonitoring];
}

@end
